package base;

import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import admin.mode.AdminMode;
import base.Power;
import base.Sql;
import base.tools.Constants;

/**
 * CMS后台DispatchAction基类
 * 
 * @author qsj
 */

public class AdminDispatchAction extends DispatchAction {
	protected Class[] types = { base.Power.class, admin.mode.AdminMode.class };

	protected Method getMethod(String name) throws NoSuchMethodException {

		synchronized (methods) {
			Method method = (Method) methods.get(name);
			if (method == null) {
				method = clazz.getMethod(name, types);
				methods.put(name, method);
			}
			return (method);
		}

	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html;charset=GBK");
		String parameter = mapping.getParameter();
		String name = request.getParameter(parameter);
		if ("execute".equals(name) || "perform".equals(name)) {
			String message = messages.getMessage("dispatch.recursive", mapping
					.getPath());
			// log.error(message);
			throw new ServletException(message);
		}
		return dispatchMethod(mapping, form, request, response, name);

	}

	protected ActionForward dispatchMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String name) throws Exception {

		if (name == null) {
			return this.unspecified(mapping, form, request, response);
		}
		Method method = null;
		try {
			method = getMethod(name);
		} catch (NoSuchMethodException e) {
			String message = messages.getMessage("dispatch.method", mapping
					.getPath(), name);
			// log.error(message, e);

			String userMsg = messages.getMessage("dispatch.method.user",
					mapping.getPath());
			throw new NoSuchMethodException(userMsg);
		}

		ActionForward forward = null;
		Sql sql = new Sql();
		try {

			Power power = new Power(mapping, form, request, response, sql);
			AdminMode admin = new AdminMode(power);

			Object args[] = { power, admin };
			// 验证登录和权限
			// if
			// (admin.checkLogin()&&!admin.getUserInfo().get("USERNAME").equals("guest"))
			// {// 验证登录
			if (admin.checkLogin()
					&& admin.getUserInfo().get("IS_ADMIN").equals("1")) {
				String opt = power.getParameter("opt");
				boolean viewOnly = false;
				if (admin.getUserInfo().get("IS_VIEWONLY_ADMIN").equals("1")) {
					viewOnly = true;
				}
				if (admin.permistionPass(power, opt, admin.getUserInfo(),
						viewOnly)) {// 验证权限
					Date d1 = new Date();
					forward = (ActionForward) method.invoke(this, args);
					Date d2 = new Date();
					long timenum = d2.getTime() - d1.getTime();
					long statnum = sql.getStatNum();
					if (timenum > 500) {// 超过0.5秒钟,记录日至

					}
				} else {
					// 转到无权限提示页
					power.debug("无权限");
				}

			} else {
				response.getWriter().println("<script language=javascript>");
				response.getWriter().println(
						"this.top.location='" + Constants.PREFIX_PATH
								+ "/admin/login.jsp';");
				response.getWriter().println("</script>");
			}

		} catch (Exception e) {
			response.getWriter().println(
					"<%@ page contentType=\"text/html; charset=GBK\" %>");
			response.getWriter().println("系统错误" + "<!--");
			e.printStackTrace(response.getWriter());
			response.getWriter().println("-->");
		}

		finally {
			sql.close();
		}

		// Return the returned ActionForward instance
		return (forward);
	}
}
